﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using EquipmentConfig.Class;

namespace EquipmentConfig
{
    public partial class SetDeviceTime : Form
    {
        private MainForm mf;
        public SetDeviceTime(MainForm mainForm)
        {
            InitializeComponent();
            mf = mainForm;
        }

        private void SetDeviceTime_Load(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            dateEdit1.EditValue = dt.ToString("yyyy/MM/dd");
            timeEdit1.EditValue = dt.ToString("HH:mm:ss");
        }

        /// <summary>
        /// 刷新时间
        /// </summary>
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (checkEdit1.Checked)
            {
                DateTime dt = DateTime.Now;
                dateEdit1.EditValue = dt.ToString("yyyy/MM/dd");
                timeEdit1.EditValue = dt.ToString("HH:mm:ss");
            }
        }

        /// <summary>
        /// 使用系统时间选择
        /// </summary>
        private void checkEdit1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEdit1.Checked)
            {
                DateTime dt = DateTime.Now;
                dateEdit1.EditValue = dt.ToString("yyyy/MM/dd");
                timeEdit1.EditValue = dt.ToString("HH:mm:ss");
                dateEdit1.Enabled = false;
                timeEdit1.Enabled = false;
            }
            else
            {
                dateEdit1.Enabled = true;
                timeEdit1.Enabled = true;
            }
        }

        /// <summary>
        /// 设置时间
        /// </summary>
        private void btnSetTime_Click(object sender, EventArgs e)
        {
            try
            {
                mf.timer2.Enabled = false;
                mf.recBuffer.Clear();
                MainForm.isSuccess = false;
                byte[] byteDate = new byte[6];
                DateTime date1 = Convert.ToDateTime(dateEdit1.Text);
                byteDate[0] = Convert.ToByte(date1.Year.ToString().Substring(2, 2));
                byteDate[1] = Convert.ToByte(date1.Month);
                byteDate[2] = Convert.ToByte(date1.Day);
                byteDate[3] = Convert.ToByte(timeEdit1.Time.Hour);
                byteDate[4] = Convert.ToByte(timeEdit1.Time.Minute);
                byteDate[5] = Convert.ToByte(timeEdit1.Time.Second);
                mf.SendCmd(Protocol.GetWriteDeviceCmd(0x06, byteDate));

                Thread sub = new Thread(() =>
                {
                    if (mf.CheckReturnCmd())
                    {
                        Messages.ShowAlertDialog("设置设备时间成功！", true);
                    }
                    else
                    {
                        Messages.ShowAlertDialog("设置时间失败！", false);
                    }
                });
                sub.IsBackground = true;
                sub.Start();
                mf.timer2.Enabled = true;
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog("设置时间错误！", ex);
            }
        }

        private void SetDeviceTime_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Dispose();
        }
    }
}
